package basic.JUC.print100;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/9 14:45
 * @描述 暂无
 */
public class SwapPrint {
    public static final Object lock = new Object();

    public static void main(String[] args) {
        new Thread(new A()) {
        }.start();

        new Thread(new B()) {
        }.start();
    }

    static class A implements Runnable {
        public void run() {
            for (int i = 1; i <= 100; i++) {
                synchronized (lock) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ": " + i);
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.notify();
                }
            }
        }
    }

    static class B implements Runnable {
        public void run() {
            for (int i = 1; i <= 100; i++) {
                synchronized (lock) {
                    if (i % 2 == 1) {
                        System.out.println(Thread.currentThread().getName() + ": " + i);
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    lock.notify();
                }
            }
        }
    }
}
